package top.cmx.learn.thread.test;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * @author cmx
 */
public class CarStock {
    private static int count = 0;
    /**
     * 共享缓冲区
     */
    private BlockingDeque<CarData> queue;

    public CarStock(BlockingDeque<CarData> queue) {
        this.queue = queue;
    }

    /**
     * 生产车
     */
    public synchronized void productCar() {
        try {
            CarData carData = new CarData();
            //向CarData队列增加一个CarData对象
            boolean success = this.queue.offer(carData, 2, TimeUnit.SECONDS);
            if (success) {
                int id = ++count;
                carData.setId(id);
                System.out.println("生产CarData, 编号:" + id + ",库存:" + queue.size());
                Thread.sleep((int) (1000 * Math.random()));
                notifyAll();
            } else {
                System.out.println("生产CarData失败");
            }
            if (queue.size() < 100) {

            } else {
                System.out.println("库存已满,等待消费");
                wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 消费车
     */
    public synchronized void consumeCar() {

        try {
            CarData carData = this.queue.poll(2, TimeUnit.SECONDS);
            if (carData != null) {
                Thread.sleep((int) (1000 * Math.random()));
                notifyAll();
                System.out.println("消费CarData, 编号:" + carData.getId() + ",库存:" + queue.size());
            } else {
                System.out.println("消费CarData失败");
            }
            if (queue.size() > 0) {

            } else {
                System.out.println("库存为空,等待生产……");
                wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
